package com.lanyan.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.lanyan.common.core.domain.BaseEntity;
import com.lanyan.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.lanyan.common.utils.StringUtils;
import com.lanyan.system.mapper.AccountAddressMapper;
import com.lanyan.system.domain.AccountAddress;
import com.lanyan.system.domain.vo.AccountAddressVo;
import com.lanyan.system.service.IAccountAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 用户地址Service业务层处理
 *
 * @author lanyan
 * @date 2024-11-26
 */
@Service
public class AccountAddressServiceImpl  extends ServiceImpl<AccountAddressMapper,AccountAddress> implements IAccountAddressService

{
    @Autowired
    private AccountAddressMapper accountAddressMapper;

    /**
     * 查询用户地址
     *
     * @param accountAddressId 用户地址主键
     * @return 用户地址
     */
    @Override
    public AccountAddress selectAccountAddressByAccountAddressId(Long accountAddressId)
    {
        return this.getById(accountAddressId);
    }

    /**
     * 查询用户地址Vo列表
     *
     * @param accountAddress 用户地址
     * @return 用户地址
     */
    @Override
    public List<AccountAddressVo> selectAccountAddressVoList(AccountAddress accountAddress)
    {
        return selectAccountAddressList(accountAddress)
                .stream()
                .map(x -> (AccountAddressVo)x)
                .collect(Collectors.toList());
    }
    /**
     * 查询用户地址列表
     *
     * @param accountAddress 用户地址
     * @return 用户地址
     */
    @Override
    public List<AccountAddress> selectAccountAddressList(AccountAddress accountAddress)
    {
        LambdaQueryChainWrapper<AccountAddress> wrapper = this.lambdaQuery().eq(AccountAddress::getDelFlag,"0");
        if (accountAddress.getAccountId() != null) {
            wrapper.eq(AccountAddress::getAccountId, accountAddress.getAccountId());
        }
        if (StringUtils.isNotEmpty(accountAddress.getName())) {
            wrapper.like(AccountAddress::getName, accountAddress.getName());
        }
        if (StringUtils.isNotEmpty(accountAddress.getPhone())) {
            wrapper.eq(AccountAddress::getPhone, accountAddress.getPhone());
        }
        if (accountAddress.getEstateId() != null) {
            wrapper.eq(AccountAddress::getEstateId, accountAddress.getEstateId());
        }
        if (accountAddress.getBuildingId() != null) {
            wrapper.eq(AccountAddress::getBuildingId, accountAddress.getBuildingId());
        }
        if (StringUtils.isNotEmpty(accountAddress.getDetails())) {
            wrapper.eq(AccountAddress::getDetails, accountAddress.getDetails());
        }
        if (StringUtils.isNotEmpty(accountAddress.getIsDefault())) {
            wrapper.eq(AccountAddress::getIsDefault, accountAddress.getIsDefault());
        }
        wrapper.orderByDesc(BaseEntity::getCreateTime);
        return wrapper.list();
    }

        /**
         * 新增用户地址
         *
         * @param accountAddress 用户地址
         * @return 结果
         */
    @Override
        public boolean insertAccountAddress(AccountAddress accountAddress)
        {
                    accountAddress.setCreateTime(DateUtils.getNowDate());
                return this.save(accountAddress);
        }

        /**
         * 修改用户地址
         *
         * @param accountAddress 用户地址
         * @return 结果
         */
    @Override
        public boolean updateAccountAddress(AccountAddress accountAddress)
        {
                    accountAddress.setUpdateTime(DateUtils.getNowDate());
            return this.updateById(accountAddress);
        }

        /**
         * 批量删除用户地址
         *
         * @param accountAddressIds 需要删除的用户地址主键
         * @return 结果
         */
    @Override
        public boolean deleteAccountAddressByAccountAddressIds(Long[] accountAddressIds)
        {
            return this.lambdaUpdate().set(AccountAddress::getDelFlag,"1").in(AccountAddress::getAccountAddressId,accountAddressIds).update();
        }

        /**
         * 删除用户地址信息
         *
         * @param accountAddressId 用户地址主键
         * @return 结果
         */
    @Override
        public boolean deleteAccountAddressByAccountAddressId(Long accountAddressId)
        {
            return this.lambdaUpdate().set(AccountAddress::getDelFlag,"1").eq(AccountAddress::getAccountAddressId,accountAddressId).update();
        }
    }
